package com.zone.vehicle.lib.view.captcha.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.zone.vehicle.lib.view.R;
import com.zone.vehicle.lib.view.captcha.model.CaptchaGetIt;
import com.zone.vehicle.lib.view.utils.ImageUtil;


/**
 * Date:2020/5/19
 * author:wuyan
 */
public class BlockPuzzleDialog extends Dialog {
    private String baseImageBase64;//背景图片
    private String slideImageBase64;//滑动图片
    private String token;
    private Context mContext;
    private TextView tvDelete;
    private ImageView tvRefresh;
    private DragImageView dragView;
    private Handler handler = new Handler();
    private String key;

    private CaptchaGetIt data;
    private DragImageView.DragListenner dragListenner;


    public BlockPuzzleDialog(@NonNull Context context) {
        super(context, R.style.dialog);
        this.mContext = context;
        setContentView(R.layout.dialog_block_puzzle);
        getWindow().setGravity(Gravity.CENTER);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        initView();
//        loadCaptcha();
        initEvent();
        WindowManager windowManager = ((Activity) mContext).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        ViewGroup.LayoutParams lp = getWindow().getAttributes();
        lp.width = display.getWidth() * 9 / 10;
        getWindow().setAttributes((WindowManager.LayoutParams) lp);
        setCanceledOnTouchOutside(false);//点击外部Dialog不消失
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public void setData(CaptchaGetIt data) {
        this.data = data;
        baseImageBase64 = data.getOriginalImageBase64();
        slideImageBase64 = data.getJigsawImageBase64();
        token = data.getToken();
        key = data.getSecretKey();
        dragView.setUp(ImageUtil.base64ToBitmap(baseImageBase64), ImageUtil.base64ToBitmap(slideImageBase64));
        dragView.setSBUnMove(true);

    }

    private void initView() {
        tvDelete = findViewById(R.id.tv_delete);
        tvRefresh = findViewById(R.id.tv_refresh);
        dragView = findViewById(R.id.dragView);

        Bitmap bitmap = ImageUtil.getBitmap(getContext(), R.drawable.bg_default);
        dragView.setUp(bitmap, bitmap);
        dragView.setSBUnMove(false);
    }

    public void setDragListenner(DragImageView.DragListenner dragListenner) {
        this.dragListenner = dragListenner;
        dragView.setDragListenner(dragListenner);
    }

    public void ok(){
        dragView.ok();
    }

    public void fail(String msg){
        dragView.fail(msg);
    }

    private void checkCaptcha(double sliderXMoved) {
//        Point point = new Point();
//        point.setY(5.0);
//        point.setX(sliderXMoved);
//        String pointStr = new Gson().toJson(point);
//        Map<String, Object> params = new HashMap<>();
//        params.put("captchaType", "blockPuzzle");
//        params.put("token", token);
//        params.put("pointJson", AESUtil.encode(pointStr, key));
//        JSONObject jsonObject = new JSONObject(params);
//        RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString());
//        RetrofitUtils.getServerApi().checkAsync(body).compose(RxHelper.observableIO2Main(mContext)).subscribe(new BaseObserver<CaptchaCheckIt>(mContext, false) {
//            @Override
//            public void onSuccess(CaptchaCheckIt data) {
//                dragView.ok();
//                loadCaptcha();
//                handler.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        dismiss();
//                    }
//                }, 1500);
//
//                if (mOnResultsListener!=null){
//                    String result=token+"---"+pointStr;
//                    mOnResultsListener.onResultsClick(AESUtil.encode(result,key));
//                }
//            }
//
//            @Override
//            public void onFailure(Throwable e, String errorMsg) {
//                dragView.fail();
//                //刷新验证码
//                loadCaptcha();
//            }
//        });

    }

    private void initEvent() {
//        dragView.setDragListenner(new DragImageView.DragListenner() {
//            @Override
//            public void onDrag(double position) {
//                checkCaptcha(position);
//            }
//        });
//        dragView.setDragListenner(dragListenner);
        tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        tvRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                loadCaptcha();
            }
        });
    }

//    private OnResultsListener mOnResultsListener;

    public interface OnResultsListener {
        void onResultsClick(String result);
    }

//    public void setOnResultsListener(OnResultsListener mOnResultsListener) {
//        this.mOnResultsListener = mOnResultsListener;
//    }
}
